package com.tbit.main.controller;

import cn.hutool.core.util.NumberUtil;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.entity.view.PageResult;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.MovePerformance;
import com.tbit.main.pojo.maintain_performance.stat.*;
import com.tbit.main.service.TokenService;
import com.tbit.main.service.impl.MovePerformanceService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * 运维端挪车绩效
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/3/6
 */
@AllArgsConstructor
@RestController
@RequestMapping("/movePerformance")
public class MovePerformanceController {
    final MovePerformanceService performanceService;
    final TokenService tokenService;

    /**
     * 查询挪车绩效概览
     *
     * @param req 请求参数
     */
    @PostMapping("/queryOverviewPage")
    public JsonResult<List<PerformanceOverviewResp>> queryOverview(@Valid PerformanceOverviewReq req,String token) {
        AccountUser accountUser = tokenService.getAccount(token);
        return JsonResult.succ(performanceService.queryOverviewPage(req,accountUser));
    }

    /**
     * 查询挪车绩效统计
     *
     * @param req 请求参数
     */
    @PostMapping("/queryPerformanceCount")
    public JsonResult<PerformanceCountResp> queryPerformanceCount(@Valid PerformanceCountReq req) {
        return JsonResult.succ(performanceService.queryPerformanceCount(req));
    }

    /**
     * 查询挪车绩效详情
     *
     * @param req 请求参数
     */
    @PostMapping("/queryDetailPage")
    public JsonResult<PageResult<MovePerformanceDetail>> queryPerformanceDetail(@Valid PerformanceDetailReq req) {
        return JsonResult.succ(performanceService.queryDetailPage(req));
    }

    /**
     * 更新挪车绩效
     *
     * @param performanceId 挪车绩效id
     * @param performance   更新后的绩效值
     * @param remark        备注
     */
    @PutMapping("/updatePerformance")
    @OperateLog(module = ModuleEnum.PERFORMANCE, methods = MethodEnum.PERFORMANCE_MOVE_UPDATE)
    public JsonResult<Void> updatePerformance(@RequestParam Long performanceId,
                                              @RequestParam BigDecimal performance,
                                              @RequestParam(required = false) String remark) {
        AccountUser loginUser = LoginUserContextHolder.getLoginUser();
        if (loginUser == null) {
            throw new BaseException("未登录");
        }

        performanceService.updatePerformance(performanceId, performance, remark);
        return JsonResult.succ();
    }
}
